package cn.com.bluemoon.daps.system.service.impl;

import cn.com.bluemoon.daps.common.constant.DapConstant;
import cn.com.bluemoon.daps.system.entity.DapSystemDepartment;
import cn.com.bluemoon.daps.system.mapper.DapSystemDepartmentMapper;
import cn.com.bluemoon.daps.system.service.DapSystemDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门管理表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-08-03
 */
@Slf4j
@Service
public class DapSystemDepartmentServiceImpl extends ServiceImpl<DapSystemDepartmentMapper, DapSystemDepartment> implements DapSystemDepartmentService {

    @Override
    @Cacheable(cacheNames = DapConstant.DapSystem.CACHE_NAME, keyGenerator = DapConstant.CACHE_KEY_GENERATOR)
    public Map<String, String> dbDistinctDepartAndIdMap() {
        return list(bmLambdaQuery())
                .stream().collect(Collectors.toMap(DapSystemDepartment::getDepartName, DapSystemDepartment::getId, (a, b) -> b));
    }

    @Override
    @Cacheable(cacheNames = DapConstant.DapSystem.CACHE_NAME, keyGenerator = DapConstant.CACHE_KEY_GENERATOR)
    public DapSystemDepartment getById(Serializable id) {
        return super.getById(id);
    }

    /**
     * 重写获取部门信息，加入缓存
     *
     * @return
     */
    @Override
    @Cacheable(cacheNames = DapConstant.DapSystem.CACHE_NAME, keyGenerator = DapConstant.CACHE_KEY_GENERATOR)
    public List<DapSystemDepartment> list() {
        return list(bmLambdaQuery());
    }
}
